package com.molyfun.parents.activity.store.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.classroom.ClassGoodViewHolder;
import com.molyfun.parents.activity.course.CoursePickActivity;
import com.molyfun.parents.activity.course.CourseSearchActivity;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.activity.setting.MineActivity;
import com.molyfun.parents.activity.store.view.ProductView;
import com.molyfun.parents.impl.IHorizontalTag;
import com.molyfun.parents.rest.bean.TeamListResponse;
import com.molyfun.parents.view.HorizonalTwoTabBar;
import com.molyfun.parents.view.HorizontalPickBar;
import com.molyfun.parents.view.TwoTabBar;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class ProductHeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<SubjectListResponse.SubjectBrief> mList;
    private List<TeamListResponse.TeamBean> mClassCourseGoodsList;
    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_CONTENT = 1002;
    private static final int TYPE_CLASS_COURSE = 1003;

    private int mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;

    private List<IHorizontalTag> mHeaderList;

    public ProductHeaderAdapter(Context context, ClassCenterFragmentInterface classCenterFragmentInterface) {
        this.mContext = context;
        this.mClassCenterFragmentInterface = classCenterFragmentInterface;
    }

    public void refreshData(List<SubjectListResponse.SubjectBrief> list) {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
        this.mList = list;
        notifyDataSetChanged();
    }

    public void refreshClassCourseData(List<TeamListResponse.TeamBean> list) {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_TWO;
        this.mClassCourseGoodsList = list;
        notifyDataSetChanged();
    }

    public void clearAll() {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void clearAllClassCourse() {
        mCurrentChooseTab = TwoTabBar.TwoTabChooseTab.BAR_TWO;
        if (mClassCourseGoodsList != null) {
            mClassCourseGoodsList.clear();
        }
        notifyDataSetChanged();
    }

    public void setHeaderList(List<IHorizontalTag> headerList) {
        this.mHeaderList = headerList;
        notifyDataSetChanged();
    }

    public void addAll(List<SubjectListResponse.SubjectBrief> list) {
        if (mList != null) {
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addAllClassCourse(List<TeamListResponse.TeamBean> list) {
        if (mClassCourseGoodsList != null) {
            this.mClassCourseGoodsList.addAll(list);
            notifyDataSetChanged();
        }
    }

    private ClassCenterFragmentInterface mClassCenterFragmentInterface;

    public interface ClassCenterFragmentInterface {
        void onTwoBarClick(int chooseTab);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            if (mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE) {
                return TYPE_CONTENT;
            } else {
                return TYPE_CLASS_COURSE;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER:
                return new ProductHeaderViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_class_center_title, parent, false));
            case TYPE_CLASS_COURSE:
                return new ClassGoodViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_class_good_cell, parent, false));
            case TYPE_CONTENT:
            default:
                return new ProductBriefViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_product_cell, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                ProductHeaderViewHolder productHeaderViewHolder = (ProductHeaderViewHolder) holder;
                productHeaderViewHolder.mTwoTabBar.initData("公开课", "班级");
                productHeaderViewHolder.mTwoTabBar.setTwoBarClickListener(new HorizonalTwoTabBar.TwoBarClickListener() {
                    @Override
                    public void onTwoBarClick(int chooseTab) {
                        if(mClassCenterFragmentInterface != null){
                            mClassCenterFragmentInterface.onTwoBarClick(chooseTab);
                        }
                    }
                });
                if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
                    productHeaderViewHolder.mPickLayout.setVisibility(View.VISIBLE);
                    productHeaderViewHolder.mHorizontalTagBar.initDataList(mHeaderList);
                }else{
                    productHeaderViewHolder.mPickLayout.setVisibility(View.GONE);
                }
                break;
            case TYPE_CONTENT:
                final SubjectListResponse.SubjectBrief productBean = mList.get(position - 1);
                ProductBriefViewHolder productBriefViewHolder = (ProductBriefViewHolder) holder;
                productBriefViewHolder.mProductView.initData(productBean);
                break;
            case TYPE_CLASS_COURSE:
                ClassGoodViewHolder classGoodViewHolder = (ClassGoodViewHolder) holder;
                classGoodViewHolder.mClassGoodView.initData(mClassCourseGoodsList.get(position - 1));
                break;
        }
    }

    public class ProductBriefViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.product_layout)
        ProductView mProductView;

        public ProductBriefViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class ProductHeaderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @InjectView(R.id.pick_btn)
        TextView mPickBtn;
        @InjectView(R.id.horizontal_tag_bar)
        HorizontalPickBar mHorizontalTagBar;
        @InjectView(R.id.profile_icon)
        ImageView mProfileIcon;
        @InjectView(R.id.search_icon)
        ImageView mSearch;
        @InjectView(R.id.two_tab_bar)
        HorizonalTwoTabBar mTwoTabBar;
        @InjectView(R.id.pick_layout)
        LinearLayout mPickLayout;

        public ProductHeaderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);

            mProfileIcon.setOnClickListener(this);
            mSearch.setOnClickListener(this);
            mPickBtn.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.profile_icon:
                    MineActivity.startActivity(mContext);
                    break;
                case R.id.search_icon:
                    CourseSearchActivity.startActivity(mContext);
                    break;
                case R.id.pick_btn:
                    CoursePickActivity.startActivity(mContext, CoursePickActivity.FLAG_FROM_MAIN_TAB);
                    break;
            }
        }
    }


    @Override
    public int getItemCount() {
        if(mCurrentChooseTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
            if (mList == null || mList.size() == 0) {
                return 1;
            }
            return mList.size() + 1;
        }else{
            if (mClassCourseGoodsList == null || mClassCourseGoodsList.size() == 0) {
                return 1;
            }
            return mClassCourseGoodsList.size() + 1;
        }
    }

}
